package com.thf.gulimall.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

@Configuration
public class CoreConfiguration {
    @Bean
    public CorsWebFilter corsWebFilter() {
        CorsConfiguration config = new CorsConfiguration();
        config.addAllowedOriginPattern("*"); // 允许来自 http://example.com 的请求
        config.addAllowedMethod("*"); // 允许 GET 请求方法
        config.addAllowedHeader("*"); // 允许所有头信息
        config.setAllowCredentials(true); // 是否允许发送 cookie

        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", config); // 对所有路径应用 CORS 配置

        return new CorsWebFilter(source);
    }
}
